define(
    function (require) {
        require('tpl!./list.tpl.html');
        var util = require('common/util');
        var u = require('underscore');



        var showid = 1;
        var page = 1;

        var fields = [
            {
                title: '编号',
                field: 'id' ,
                sortable: true,
                resizable: true,
                width: 30,
                content: function (item) {
                    return showid++;
                }
            },
            {
                title: '手机号码',
                field: 'phone' ,
                resizable : true,
                width: 80,
                content: function (item) {
                    return item.phone;
                }
            },
            {
                title: '设备编号',
                field: 'sbno',
                resizable : true,
                width: 80,
                content: function (item) {
                    return item.sbno;
                }
            },
            {
                title: '检测状态',
                field: 'ostatus',
                width: 80,
                content: function (item) {
                    return decidedate(item.ostatus);
                }
            },
            {
                title: '登陆时间',
                field: 'addtime',
                width: 100,
                content: function (item) {
                    return textdate(item.addtime);
                }
            },
            {
                title: '支付状态',
                field: 'rstatus',
                width: 60,
                content: function (item) {
                    return paydate(item.rstatus);
                }
            },
            {
                title: '支付类型',
                field: 'paytype' ,
                width: 100,
                content: function (item) {
                    return payType(item.paytype);
                }
            },
            // {
            //     title: '电子券号',
            //     field: 'number' ,
            //     width: 100,
            //     content: function (item) {
            //         if(item.paytype===2){
            //             if(item.number !== null){
            //                 return item.number;
            //             }
            //             return "--";
            //         }else{
            //             return "--";
            //         }
            //
            //     }
            // },
            {
                title: '主要风险',
                field: 'zname',
                resizable : true,
                width: 100,
                content: function (item) {
                    if(item.zname !== null){
                        var operates = [
                            '<p class="list-tt" title="'
                            + item.zname
                            + '" >'+item.zname+'</p>',
                        ];
                        return operates.join('');
                    }else {
                        return "-- -- --";
                    }
                    // return item.zname;
                }
            },
            {
                title: '次要风险',
                field: 'cname',
                resizable : true,
                width: 100,
                content: function (item) {
                    if(item.cname !== null){
                        var operates = [
                            '<p class="list-tt" title="'
                            + item.cname
                            + '" >'+item.cname+'</p>',
                        ];
                        return operates.join('');
                    }else {
                        return "-- -- --";
                    }
                     // return item.cname;
                }
            },
            {
                title: '性别',
                field: 'gender' ,
                width: 80,
                content: function (item) {
                    return sexdate(item.gender);
                }
            },
            {
                title: '年龄',
                field: 'age' ,
                width: 80,
                content: function (item) {
                    if(item.age !== null){
                        return item.age;
                    }
                    return "--";
                }
            },
            {
                title: '操作',
                field: 'operate' ,
                width: 80,
                content: function (item) {
                    if(item.pdfurl !== null && item.pdfurl !==""){
                        var operates = [
                            '<a class="list-link" href="'
                            + item.pdfurl
                            + '" target="view_window">查看报告</a>',
                        ];
                        return operates.join('');
                    }else{
                        return "--";
                    }

                }
            }
        ];

        //时间戳转换
        function  textdate(obj) {
            var date = new Date(obj);
            var seperator1 = "-";
            var seperator2 = ":";
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            var minuts = date.getMinutes()<10?"0"+date.getMinutes():date.getMinutes();
            var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
                + " " + date.getHours() + seperator2 + minuts
                + seperator2 + date.getSeconds();
            return currentdate;
        }
        //体检状态
        function  decidedate(obj){
            if(obj===0){
                return "开始";
            }else if(obj===1){
                return "完成";
            }else if(obj===2){
                return "用户放弃";
            }else if(obj===3){
                return "检测失败";
            }else if(obj===4){
                return "检测失败";
            }else{
                return "--";
            }
        }
        //支付状态
        function paydate(obj) {
            if(obj===0){
                return "未支付";
            }else if(obj===1){
                return "支付成功";
            }else if(obj===2){
                return "支付失败";
            }else{
                return "--";
            }
        }
        //支付类型
        function payType(obj){
            if(obj===0){
                return "未支付";
            }else if(obj===1){
                return "现金支付";
            }else if (obj===2){
                return "票券支付";
            }else {
                return "--";
            }
        }
        //性别
        function sexdate(obj) {
            if(obj===0){
                return "男";
            }else if(obj===1){
                return "女";
            }else{
                return "--";
            }
        }

        function search() {
            var phone = this.get('reportName');
            var zname = this.get('zName');
            var cname = this.get('cName');
            page = 1;
            if(phone.getValue().length>0){
                if(zname.getValue().length>0 || cname.getValue().length>0){
                    alert("不能与其他选项同时选择");
                } else{
                    if(phone.getValue().length === 11){
                        this.fire('search', { phone: phone.getValue(),zname: zname.getValue(),cname:cname.getValue() });
                    }else{
                        alert("手机号码必须为11位！");
                    }
                }
            }else {
                if(zname.getValue().length <=0 || cname.getValue().length<0){
                    alert("检索项不能全为空");
                } else{
                    this.fire('search', {phone: phone.getValue(), zname: zname.getValue(),cname:cname.getValue() });
                }
            }

        }

        //分页
        function flip(e) {
            var page = e.target.getAttribute('data-page');
            if (/(\+|\-)/.test(page)) {
                page = parseInt(page) + (RegExp.$1 === '+' ? 1 : -1);
            }
            this.fire('flip', { page: page});
        }
        
        var BaseListView = require('common/ListView');

        function ReportListView() {
            BaseListView.apply(this, arguments);
        }

        ReportListView.prototype.template = 'reportListPage';

        ReportListView.prototype.getUIProperties = function () {
            BaseListView.prototype.getUIProperties.apply(this, arguments);

            var str = location.href;
            var num = str.indexOf("=");
            if(num === -1){
                page = 1;
            } else {
                var name = str.substring(num-4, num);
                if(name === 'page'){
                    page = str.substring(num + 1, str.length);
                }else {
                    page = 1;
                }
            }
            showid = (page-1)*10+1;



            return {
                list: {
                    fields: fields,
                    sortable: false,
                    columnResizable: true,
                    subrow: false,
                    followHead: true,
                    selectMode: 'line',
                    noDataHtml: '<p style="margin:0;">暂无报告信息!</p>'
                }
            };
        }


        ReportListView.prototype.enterDocument = function () {
            BaseListView.prototype.enterDocument.apply(this, arguments);

            var me = this;
            this.get('list').on(
                'command',
                function (event) {
                    me.fire(event.name, event);
                }
            );

            this.get('createButton').on(
                'click',
                this.fire.bind(this, 'create')
            );

            this.get('searchButton').on(
                'click',
                util.bind(search, this)
            );

            $('#list-page').on(
                'click',
                ':not(.disable)',
                util.bind(flip, this)
            );

            this.get('list').initChildren();
        }

        require('er/util').inherits(ReportListView, BaseListView);
        return ReportListView;
    }
);
